package com.liang.blog.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.blog.common.exception.BlogException;
import com.liang.blog.common.result.Result;
import com.liang.blog.common.result.ResultCodeEnum;
import com.liang.blog.model.BlogUser;
import com.liang.blog.model.base.BlogPageInfo;
import com.liang.blog.security.annotation.HasPermission;
import com.liang.blog.security.utils.BlogSecurityUtil;
import com.liang.blog.system.service.BlogUserService;
import com.liang.blog.vo.user.LoginUserVo;
import com.liang.blog.vo.user.RegisterVo;
import com.liang.blog.vo.user.UserInfoVo;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2023-08-14-20:37
 */
//@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class BlogUserController {

    @Autowired
    private BlogUserService blogUserService;

//    @ApiOperation("获取用户列表")
    @GetMapping("list/{page}/{pageSize}")
    @HasPermission("system:user:read")
    public Result getUserList(@PathVariable int page,
                              @PathVariable int pageSize,
                              UserInfoVo userInfoVo){
        PageHelper.startPage(page, pageSize);
        List<UserInfoVo> userInfoPage = blogUserService.getUserInfoPage(userInfoVo);
        BlogPageInfo<UserInfoVo> blogPageInfo =
                new BlogPageInfo(new PageInfo(userInfoPage).getTotal(), userInfoPage);

        return Result.ok(blogPageInfo);
    }

//    @ApiOperation("根据用户账号获取用户信息")
    @GetMapping("getUserByInfo/{account}")
    @HasPermission("system:user:list")
    public Result<BlogUser> getUserByAccount(@PathVariable(value = "account") String account){
        BlogUser blogUser = blogUserService.getUserByAccount(account);
        return Result.ok(blogUser);
    }

    //    @ApiOperation("根据用户账号获取用户信息")
    @GetMapping("getLoginUserInfo/{account}")
    public Result<LoginUserVo> getLoginUserInfo(@PathVariable(value = "account") String account){
        LoginUserVo loginUserInfo = blogUserService.getLoginUserInfo(account);
        return Result.ok(loginUserInfo);
    }

    @GetMapping("getUserCount/{account}")
    public Result<Integer> getUserCount(@PathVariable(value = "account") String account){
        return Result.ok(blogUserService.getUserCount(account));
    }

    @GetMapping("public/getUserByEmailCount/{email}")
    public Result<Integer> getUserByEmailCount(@PathVariable(value = "email") String email){
        return Result.ok(blogUserService.getUserByEmailCount(email));
    }

    @GetMapping("public/getUserByEmail/{email}")
    Result<BlogUser> getUserByEmail(@PathVariable(value = "email") String email){
        return Result.ok(blogUserService.getUserByEmail(email));
    }

//    @ApiOperation("根据用户 id 获取用户信息")
    @GetMapping("getUserById/{id}")
//    @HasPermission("system:user:list")
    public Result<UserInfoVo> getUserById(@PathVariable(value = "id") Long id){
        UserInfoVo userInfoVo = blogUserService.getUserById(id);
        return Result.ok(userInfoVo);
    }

    @PostMapping("getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        Long userId = BlogSecurityUtil.getUserId();
        if (ObjectUtils.isEmpty(userId)){
            throw new BlogException(ResultCodeEnum.LOGIN_AUTH);
        }
        UserInfoVo userInfoVo = blogUserService.getUserById(userId);

        if (ObjectUtils.isEmpty(userInfoVo)){
            throw new BlogException(ResultCodeEnum.SERVICE_ERROR);
        }

        return Result.ok(userInfoVo);
    }

    /**
     * 逻辑删除用户
     * @return
     */
    @DeleteMapping("deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Long id){
        int i = blogUserService.deleteUserById(id);
        return Result.ok();
    }
    /**
     * 设置登入 ip
     */
    @GetMapping("setLoginIp/{ip}/{account}")
    public void setLoginIp(@PathVariable(value = "ip") String ip, @PathVariable(value = "account") String account){
        System.out.println(ip);
        blogUserService.setLoginIp(ip, new Date(), account);
    }

    /**
     * 修改用户状态
     * @return
     */
    @GetMapping("updateUserStatus/{status}/{id}")
    @HasPermission("system:user:update")
    public Result updateUserStatus(@PathVariable int status, @PathVariable String id){
        blogUserService.updateUserStatus(status, id);
        return Result.ok();
    }

    /**
     * 修改用户信息
     * @param userInfoVo
     * @return
     */
    @PostMapping("updateUserById")
    @HasPermission("system:user:update")
    public Result updateUserById(@RequestBody UserInfoVo userInfoVo){
        blogUserService.updateUserById(userInfoVo);
        return Result.ok();
    }

    /**
     * 保存用户
     * @param registerVo
     * @return
     */
    @PostMapping("saveUser")
    public Result<Boolean> saveUser(@RequestBody RegisterVo registerVo){
        return Result.ok(blogUserService.saveUser(registerVo));
    }

    /**
     * 用户更新个人信息
     * @param userInfoVo 用户信息
     * @return
     */
    @PostMapping("/updateUserForUs")
    public Result<Boolean> updateUserForUs(HttpServletRequest request, @RequestBody UserInfoVo userInfoVo){
        blogUserService.updateUserForUs(request, userInfoVo);
        return Result.ok();
    }

}
